package com.mcy.view.locateview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * 定位采集控件
 * 通过移动控件布局位置进行点对准
 * 通过获取控件中心点和布局位置
 * 计算得到对准点屏幕坐标
 * 控件外观通过布局和宽高计算绘制
 * @author Administrator
 *
 */
public class LocateToolView extends View{

	//画笔
	private Paint mPaint;
	private Paint mBasePaint;
	//点击位置（屏幕坐标系）
	private int mTouchX;
	private int mTouchY;
	private int mMoveX = 0;
	private int mMoveY = 0;
	//控件中心点（屏幕坐标系）
	public int centerX ,centerY ;

	public LocateToolView(Context context) {
		super(context);
		init();
	}
	public LocateToolView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public LocateToolView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
		mPaint = new Paint();
		mPaint.setColor(Color.BLUE);
		mPaint.setStyle(Style.STROKE);
		mPaint.setStrokeWidth(2);
		mBasePaint = new Paint();
		mBasePaint.setColor(Color.BLACK);
		mBasePaint.setStyle(Style.STROKE);
		mBasePaint.setStrokeWidth(2);
		setFocusableInTouchMode(true);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		//canvas坐标系是控件本身坐标系
		super.onDraw(canvas);
		int w = getWidth();
		int h = getHeight();
		
		canvas.drawLine(0, h/2, w/2-w/6, h/2, mBasePaint);
		canvas.drawLine(w, h/2, w/2+w/6, h/2, mBasePaint);
		canvas.drawLine(w/2, 0, w/2, h/2-h/6, mBasePaint);
		canvas.drawLine(w/2, h, w/2, h/2+h/6, mBasePaint);
		
		canvas.drawRect(w/2-w/6, h/2-h/6,
				        w/2+w/6, h/2+h/6, mBasePaint);
		
		canvas.drawLine(w/2-w/4, h/2-h/4, w/2-w/12, h/2-h/12, mBasePaint);
		canvas.drawLine(w/2+w/4, h/2+h/4, w/2+w/12, h/2+h/12, mBasePaint);
		canvas.drawLine(w/2-w/4, h/2+h/4, w/2-w/12, h/2+h/12, mBasePaint);
		canvas.drawLine(w/2+w/4, h/2-h/4, w/2+w/12, h/2-h/12, mBasePaint);
		
		canvas.drawLine(w/2, h/2-h/12, w/2, h/2+h/12, mPaint);
		canvas.drawLine(w/2-w/12, h/2, w/2+w/12, h/2, mPaint);
	}
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		//--------l,t,r,b都是相对与父视图的位置，而不是屏幕坐标-------------
		super.onLayout(changed, left, top, right, bottom);
        centerX = (left+right)/2;
        centerY = (top+bottom)/2;
	}
	
	@Override
	public boolean performClick() {
		Log.d("locateView", "on clicked");
		return super.performClick();
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mPaint.setColor(Color.RED);
			mTouchX = (int)event.getRawX();//event.getX()获取控件坐标系，而非屏幕坐标系
			mTouchY = (int)event.getRawY();//屏幕坐标系 event.getRawX()
			break;
		case MotionEvent.ACTION_UP:
			mPaint.setColor(Color.BLUE);
			mMoveX = 0;
			mMoveY = 0;
			performClick();
			break;
		case MotionEvent.ACTION_MOVE:
			mMoveX = (int)event.getRawX()-mTouchX;
			mMoveY = (int)event.getRawY()-mTouchY;
			
			mTouchX = (int) event.getRawX();
			mTouchY = (int) event.getRawY();
			
			centerX = centerX + (int) mMoveX;
			centerY = centerY + (int) mMoveY;
			//修改布局位置
			layout(centerX - getWidth() / 2, centerY - getHeight() / 2,
					centerX + getWidth() / 2, centerY + getHeight() / 2);
			
			/*-------------scroll函数滚动的事控件内容，而非控件本身坐标-------
			scrollBy(-(int)mMoveX, -(int)mMoveY);
			*/
			break;
		default:
			break;
		}
		invalidate();
		/*
		 * view onTouch()返回true后续监听不会转发到父控件,且不会自动触发onclick事件
		 * 返回false则会转发到父控件，如果不是foucused则中断接收监听 ，会自动触发onclick事件
		 */
		return true;
	}
}
